package models

import (
	"gitee.com/chenrh/finego/app"
)

type PUB_USER struct {
	UserId     int64  `gorm:"PRIMARY_KEY;AUTO_INCREMENT;NOT NULL"`
	UserCode   string `gorm:"type:varchar(50); not null;" validate:"required" cn:"用户名"`
	NickName   string `gorm:"type:varchar(36); not null;" validate:"required" cn:"昵称"`
	UserPhone  string `gorm:"type:varchar(11); null;"`
	Email      string `gorm:"type:varchar(50); null;"`
	LoginPass  string `gorm:"type:varchar(50); null;"`
	LoginSalt  string `gorm:"type:varchar(50); null;"`
	LoginLimit int64  `gorm:"not null; default:0;"`
	BaseModel
}

func (PUB_USER) TableName() string {
	return "pub_user"
}

func (this PUB_USER) GetModel(user_id int64) PUB_USER {
	db := app.GetDB()
	// defer db.Close()
	// 查询指定的某条记录(仅当主键为整型时可用)
	user := PUB_USER{}
	db.First(&user, user_id)
	//// SELECT * FROM users WHERE id = 10;
	return user
}

func (this PUB_USER) GetModelByUserCode(user_code string) (PUB_USER, error) {
	db := app.GetDB()
	// defer db.Close()
	user := PUB_USER{}
	result := db.Where("user_code = ?", user_code).First(&user)
	return user, result.Error
}

func (this PUB_USER) InsertLoginLog(logmodel PUB_USER_LOG) {
	db := app.GetDB()
	// defer db.Close()
	db.Create(&logmodel)
}
